package com.dylan.历史.java8.并发增强;

import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * Created by Dylan on 2018/7/30.
 */
public class 并行数组操作 {

    public static void main(String[] args) {
        String[] arr1 = getWords().toArray(String[]::new);

        // 对数组进行并行排序
        Arrays.parallelSort(arr1);
        System.out.println(Arrays.toString(arr1));

        // 并行的为数组赋值.其中函数会获得元素索引,并计算该位置的值
        int[] arr2 = new int[20];
        Arrays.parallelSetAll(arr2, i -> i % 10);
        System.out.println(Arrays.toString(arr2));

        // 将数组中的每个元素替换为指定关联操作前缀的累积
        int[] arr3 = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
        Arrays.parallelPrefix(arr3, (x, y) -> x * y);
        // [1, 2, 6, 24, 120, 720, 5040, 40320]
        // [1, 1*2, 1*2*3, 1*2*3*4, 1*2*3*4*5, 1*2*3*4*5*6, 1*2*3*4*5*6*7, 1*2*3*4*5*6*7*8]
        System.out.println(Arrays.toString(arr3));
    }

    private static Stream<String> getWords() {
        return Pattern.compile("[\\P{L}]+").splitAsStream("Ramesses VI was the fifth ruler of the Twentieth Dynasty of Egypt. He succeeded Ramesses V and reigned for about eight years in the mid-to-late 12th century BC before dying in his forties. Egypt lost control of its last strongholds in Canaan around the time of his reign. The pharaoh's power waned in Upper Egypt during his rule, while the high priest of Amun, Ramessesnakht, was turning Thebes in Upper Egypt into the religious capital and a second center of power on par with Pi-Ramesses in Lower Egypt, where the pharaoh resided. He was fond of cult statues of himself; more are known to portray him than any other Twentieth-Dynasty king after his father, Ramesses III. He usurped KV9, a tomb in the Valley of the Kings planned by and for Ramesses V, and had it enlarged and redecorated for himself. His mummy lay untouched for fewer than 20 years before pillagers damaged it.");
    }
}
